package com.dcy.znoa.controller.conference;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import javax.validation.Valid;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.conference.model.conferencePerson.AddConferencePersonModel;
import com.dcy.znoa.controller.conference.model.conferencePerson.SelectConferencePersonModel;
import com.dcy.znoa.controller.conference.model.conferencePerson.UpdateConferencePersonModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.conference.ConferencePerson;
import com.dcy.znoa.service.conference.ConferencePersonService;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("conferencePerson")
public class ConferencePersonController extends BaseController {
    @Autowired
    ConferencePersonService conferencePersonService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectConferencePersonModel selectConferencePersonModel) throws BusinessException, InvocationTargetException, IllegalAccessException {       
    	Oper oper = getCurrentUser();
    	selectConferencePersonModel.setCompId(oper.getCompId());
        return conferencePersonService.selectList(selectConferencePersonModel);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddConferencePersonModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        //此接口应该不需要，新增参会人员，应该在添加会议时增加：service需要

    	if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        if(model.getConferSubject().length()>50){
            return  new JsonCommon("404","会议名称长度不能大于50");
        }
        if(null == model.getOperIds() || model.getOperIds().length < 1){
            return  new JsonCommon("404","参会人员不能为空");
        }
        Oper oper = getCurrentUser();
    	model.setCreateOperName(oper.getOperName());
        model.setCompId(getCurrentUser().getCompId());
        
        ConferencePerson conferencePerson = new ConferencePerson();
        BeanUtils.copyProperties(conferencePerson, model);
        return conferencePersonService.insert(conferencePerson);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateByConfId")
    public JsonCommon updateByConfId(@Valid UpdateConferencePersonModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        //修改、禁用、启用   ==  可能不需要在此，在会议撤销时：service需要

    	if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
        
        model.setUpdateOperName(getCurrentUser().getOperName());
        ConferencePerson conferencePerson = new ConferencePerson();
        BeanUtils.copyProperties(conferencePerson, model);
        return conferencePersonService.updateByConfId(conferencePerson);
    }

    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/getConferencePersonByConfId")
    public JsonForList getConferencePersonByConfId(@Valid SelectConferencePersonModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        //我发起的参会人员明细
        //我发起的参会人员回执明细
        //我发起的参会人员签到明细
    	//查询参会人员明细、签到、回执等接口
    	if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonForList<>("404",fieldError.getDefaultMessage());
        }

    	if(null == model.getConfId() ){
            return  new JsonForList("404","会议ID不能为空");
        }
    	
        return conferencePersonService.getConferencePersonByConfId(model.getConfId());
    }
    
    
    
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/reply")
    public JsonCommon reply(@Valid UpdateConferencePersonModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
    	//参会人员回执
    	if (bindingResult.hasErrors()){
            FieldError fieldError= bindingResult.getFieldError();
            return  new JsonCommon("404",fieldError.getDefaultMessage());
        }
    	
    	if(null == model.getReplyStatus() || (model.getReplyStatus() < 1 && model.getReplyStatus() > 2)){
            return  new JsonCommon("404","回执状态错误");
        }
    	
        model.setUpdateOperName(getCurrentUser().getOperName());
        ConferencePerson conferencePerson = new ConferencePerson();
        BeanUtils.copyProperties(conferencePerson, model);
        conferencePerson.setOperId(getCurrentUser().getOperId());
        return conferencePersonService.updateReply(conferencePerson);
    }
    
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/check")
    public JsonCommon check(String qrCode) throws BusinessException, InvocationTargetException, IllegalAccessException {
    	//参会人员签到:扫码签到--通过二维码找到会议ID
    	if(null == qrCode || qrCode.isEmpty() ){
            return  new JsonCommon("404","签到需要该会议二维码");
        }
        
    	Date checkTime =new Date();
        ConferencePerson conferencePerson = new ConferencePerson();
        conferencePerson.setUpdateOperName(getCurrentUser().getOperName());
        conferencePerson.setCheckTime(checkTime);        
        conferencePerson.setOperId(getCurrentUser().getOperId());
        conferencePerson.setUpdateTime(checkTime);
        return conferencePersonService.updateCheck(conferencePerson,qrCode);
    }
    
    
    
}
